#################################
# Meta-analysis: Figure 3 
################################

# --- combine interaction data frames:
heat<-rbind(inter_activ_keep, inter_news_keep, inter_open_keep, 
            inter_eff_keep, inter_conf_keep, inter_know_keep)

# --- reshape data for plotting with neg_combi and pos_combi
heat_long <- heat %>%
  pivot_longer(
    cols = c(combi_neg, combi_pos),  # Focus on these two combination columns
    names_to = "combination_type",
    values_to = "combination"
  ) %>%
  filter(!is.na(EMM)) %>%  # Remove NA rows
  mutate(
    Interested = ifelse(InterestedNum == 0, "Uninterested", "Interested"),
    Interested = factor(Interested, levels = c("Uninterested", "Interested")),
    # Create formatted CI label
    CI_label = sprintf("[%.2f, %.2f]", CI_lower, CI_upper))


# --- filter to retain only the specified combinations:
combinations_to_keep <- c("0 0.045", "0 0.739", "0 -1.355", "0 -1.035", 
                          "1 -0.945", "1 -0.25", "1 1.215", "1 1.444")

heat_long_filtered <- heat_long %>%
  filter(combination %in% combinations_to_keep)

# --- heatmap plotting:
plot_heat_META_CI_two<-heat_long_filtered %>%
  distinct(DV, combination, Interested, .keep_all = TRUE) %>%
  mutate(
    combination = fct_relevel(
      combination,
      "0 0.045", "0 0.739", "0 -1.355", "0 -1.035",
      "1 -0.945", "1 -0.25", "1 1.215", "1 1.444"
    ),
    DV = fct_relevel(
      DV,
      "News openness (H4b; H4d)", "Political knowledge (H4a; H4c)",
      "Confidence in knowledge (H2c)", "News consumption (H2b)",
      "Political efficacy (H2a; H3)", "Political activism (H1)"
    ),
    # Add line breaks for readability
    DV = recode(
      DV,
      "News openness (H4b; H4d)" = "(H4b; H4d) News \nopenness",
      "Political knowledge (H4a; H4c)" = "(H4a; H4c) Political \nknowledge",
      "Confidence in knowledge (H2c)" = "(H2c) Confidence \nin knowledge",
      "News consumption (H2b)" = "(H2b) News \nconsumption",
      "Political efficacy (H2a; H3)" = "(H2a; H3) Political \nefficacy",
      "Political activism (H1)" = "(H1) Political \nactivism"
    ),
    CI_label = paste0(
      "[", format(round(CI_lower, 2), nsmall = 2), 
      ", ", format(round(CI_upper, 2), nsmall = 2), "]"
    )
  ) %>%
  ggplot(aes(
    x = factor(
      combination,
      levels = c(
        "0 0.045", "0 0.739", "0 -1.355", "0 -1.035",
        "1 -0.945", "1 -0.25", "1 1.215", "1 1.444"
      ),
      labels = c("HighPos", 
                 "HighNeg", 
                 "LowPos", 
                 "LowNeg", 
                 
                 "lowNeg", 
                 "lowPos", 
                 "highNeg", 
                 "highPos")
    ),
    y = DV,
    fill = EMM
  )) + scale_x_discrete(
    labels = c(
      "HighPos" = "HighPos",
      "HighNeg" = "HighNeg",
      "LowPos"  = "LowPos",
      "LowNeg"  = "LowNeg",
      "lowNeg"  = "LowNeg",
      "lowPos"  = "LowPos",
      "highNeg" = "HighNeg",
      "highPos" = "HighPos"
    )
  )+
  geom_tile(width = 0.95, height = 0.7, lwd = 3) +
  # Add EMM value
  geom_text(aes(label = round(EMM, 2)), color = "white", size = 6, vjust = -0.1, fontface = "bold") +
  # Add CI below EMM
  geom_text(aes(label = CI_label), color = "white", size = 4, vjust = 2) +
  scale_fill_viridis(name = "", option = "E") +
  facet_grid(~Interested, scales = "free_x") +
  theme_minimal() +
  labs(title = "", x = "", y = "") +
  theme(
    text = element_text(family = "Times New Roman"),
    legend.position = "bottom",
    legend.key.width = unit(3, "cm"),
    legend.title = element_text(size = 12, vjust = 1),
    legend.text = element_text(size = 12),
    axis.text.y = element_text(size = 16, vjust = 0.5, hjust = 0), # 👈 centered vertically
    axis.text.x = element_text(size = 12, vjust = 3, hjust = 0.4),
    plot.title = element_text(hjust = 0, size = 20),
    axis.ticks = element_blank(),
    strip.background = element_rect(colour = "white"),
    strip.text = element_text(size = 16),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank()
  )
print(plot_heat_META_CI_two)
ggsave(plot_heat_META_CI_two, bg = "transparent", filename="C:/Users/claffin/Dropbox/Interest Identity/heatmap META CI TWO.png", dpi=1000, height = 8, width = 15)


